package com.cityguardian.cityguardianapp;

import com.cityguardian.model.Usuario;
import com.cityguardian.threads.AsyncTaskRegister;
import com.cityguardian.utils.Utils;
import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v7.app.ActionBarActivity;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;

public class Register2Activity extends ActionBarActivity {
	
	public final static String EXTRA_MESSAGE = "com.cityguardian.cityguardian.registerActivity";
	private Usuario usuario;
	GoogleCloudMessaging gcm;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register2);
		// Show the Up button in the action bar.
		
		Intent intent = getIntent();
		Usuario usuario=(Usuario) intent.getSerializableExtra(EXTRA_MESSAGE);
		if (usuario!=null){
			this.usuario=usuario;
//			Toast.makeText(getBaseContext(), 
//					"Hola: "+usuario.getNombres(), Toast.LENGTH_LONG).show();
		}
		
		setupActionBar();
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}else {
		    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.register2, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void registrarUsuario(View view){
    	TextView address = (TextView) findViewById(R.id.editText1);
    	TextView dninumber = (TextView) findViewById(R.id.editText2);
    	TextView cellphone = (TextView) findViewById(R.id.editText3);
    	TextView phone = (TextView) findViewById(R.id.editText4);
    	TextView email = (TextView) findViewById(R.id.editText5);    	
    	
    	String add = address.getText().toString();
    	String dni = dninumber.getText().toString();
    	String cell = cellphone.getText().toString();
    	String phon = phone.getText().toString();
    	String mail = email.getText().toString();
    	
    	usuario.setDomicilio(add);
    	usuario.setDni(dni);
    	usuario.setCelular(cell);
    	usuario.setTelefono(phon);
    	usuario.setEmail(mail);
    	
       	if (Utils.haveNetworkConnection(this)){
       		gcm = GoogleCloudMessaging.getInstance(Register2Activity.this);
    		AsyncTaskRegister thread = new AsyncTaskRegister(this,gcm);
    		thread.execute(usuario);			
		}
		else{
			Toast.makeText(getBaseContext(), 
					"Intente nuevamente. No Hay Conexion a Internet!", Toast.LENGTH_LONG).show();
		}
       	
	}

}
